

from core.himesis import Himesis, HimesisPostConditionPattern
import cPickle as pickle
from uuid import UUID

class HMoveOneInputDirectApplyDiffRulesRHS(HimesisPostConditionPattern):
    def __init__(self):
        """
        Creates the himesis graph representing the AToM3 model HMoveOneInputDirectApplyDiffRulesRHS.
        """
        # Flag this instance as compiled now
        self.is_compiled = True
        
        super(HMoveOneInputDirectApplyDiffRulesRHS, self).__init__(name='HMoveOneInputDirectApplyDiffRulesRHS', num_nodes=10, edges=[])
        
        # Add the edges
        self.add_edges([(4, 0), (0, 3), (1, 6), (1, 7), (2, 8), (2, 9), (6, 3), (7, 4), (8, 4), (9, 5)])
        # Set the graph attributes
        self["mm__"] = pickle.loads("""(lp1
S'MT_post__SimpleDSLTransMM'
p2
aS'MoTifRule'
p3
a.""")
        self["MT_action__"] = """#===============================================================================
# This code is executed after the rule has been applied.
# You can access a node labelled n matched by this rule by: PostNode('n').
# To access attribute x of node n, use: PostNode('n')['x'].
#===============================================================================

pass
"""
        self["name"] = """"""
        self["GUID__"] = UUID('9b41e6ad-c96a-4d6c-9539-d528f2d52ef4')
        
        # Set the node attributes
        self.vs[0]["MT_post__associationType"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[0]["MT_label__"] = """30"""
        self.vs[0]["mm__"] = """MT_post__directLink_T"""
        self.vs[0]["GUID__"] = UUID('c17b2267-5726-4cdc-b0ae-8c40643d1941')
        self.vs[1]["MT_label__"] = """15"""
        self.vs[1]["mm__"] = """MT_post__ApplyModel"""
        self.vs[1]["GUID__"] = UUID('b531fd0c-f6ff-4266-9f34-d0bdfc9d762e')
        self.vs[2]["MT_label__"] = """16"""
        self.vs[2]["mm__"] = """MT_post__ApplyModel"""
        self.vs[2]["GUID__"] = UUID('db6301b3-e3e2-46e8-85de-bf861d333006')
        self.vs[3]["MT_pivotOut__"] = """element1"""
        self.vs[3]["MT_label__"] = """17"""
        self.vs[3]["MT_post__name"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[3]["mm__"] = """MT_post__MetaModelElement_T"""
        self.vs[3]["MT_post__classtype"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[3]["GUID__"] = UUID('078bf6b1-0a47-4287-8217-e4e6055d511e')
        self.vs[4]["MT_label__"] = """19"""
        self.vs[4]["MT_post__name"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[4]["mm__"] = """MT_post__MetaModelElement_T"""
        self.vs[4]["MT_post__classtype"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[4]["GUID__"] = UUID('8511890c-500a-4bdf-8184-93a1690caeb5')
        self.vs[5]["MT_pivotOut__"] = """element2"""
        self.vs[5]["MT_label__"] = """18"""
        self.vs[5]["MT_post__name"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[5]["mm__"] = """MT_post__MetaModelElement_T"""
        self.vs[5]["MT_post__classtype"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[5]["GUID__"] = UUID('2fa7b2ff-0c3c-41f2-abc0-f9950ae36e17')
        self.vs[6]["MT_label__"] = """20"""
        self.vs[6]["mm__"] = """MT_post__apply_contains"""
        self.vs[6]["GUID__"] = UUID('3f0fb243-e163-4a21-a499-e325325b2d95')
        self.vs[7]["MT_label__"] = """29"""
        self.vs[7]["mm__"] = """MT_post__apply_contains"""
        self.vs[7]["GUID__"] = UUID('783e7bd7-6e19-401a-bacc-98802c33ff29')
        self.vs[8]["MT_label__"] = """22"""
        self.vs[8]["mm__"] = """MT_post__apply_contains"""
        self.vs[8]["GUID__"] = UUID('e937eb4f-ddbc-4ae0-bd85-f50dde82274a')
        self.vs[9]["MT_label__"] = """21"""
        self.vs[9]["mm__"] = """MT_post__apply_contains"""
        self.vs[9]["GUID__"] = UUID('11c730a2-c319-4712-a6da-9885741d094b')

        from HMoveOneInputDirectApplyDiffRulesLHS import HMoveOneInputDirectApplyDiffRulesLHS
        self.pre = HMoveOneInputDirectApplyDiffRulesLHS()
    
    def action(self, PostNode, graph):
        """
            Executable constraint code. 
            @param PostNode: Function taking an integer as parameter
                             and returns the node corresponding to that label.
        """
        #===============================================================================
        # This code is executed after the rule has been applied.
        # You can access a node labelled n matched by this rule by: PostNode('n').
        # To access attribute x of node n, use: PostNode('n')['x'].
        #===============================================================================
        
        pass

    def execute(self, packet, match):
        """
            Transforms the current match of the packet according to the rule %s.
            Pivots are also assigned, if any.
            @param packet: The input packet.
            @param match: The match to rewrite.
        """
        graph = packet.graph
        
        # Build a dictionary {label: node index} mapping each label of the pattern to a node in the graph to rewrite.
        # Because of the uniqueness property of labels in a rule, we can store all LHS labels
        # and subsequently add the labels corresponding to the nodes to be created.
        labels = match.copy()
        
        #===============================================================================
        # Update attribute values
        #===============================================================================
        
        #===============================================================================
        # Create new nodes
        #===============================================================================
        # directLink_T30
        new_node = graph.add_node()
        labels['30'] = new_node
        graph.vs[new_node][Himesis.Constants.META_MODEL] = 'directLink_T'
        # apply_contains29
        new_node = graph.add_node()
        labels['29'] = new_node
        graph.vs[new_node][Himesis.Constants.META_MODEL] = 'apply_contains'
        
        #===============================================================================
        # Create new edges
        #===============================================================================
        # ApplyModel15 -> apply_contains29
        graph.add_edges((labels['15'], labels['29']))
        # apply_contains29 -> MetaModelElement_T19
        graph.add_edges((labels['29'], labels['19']))
        # MetaModelElement_T19 -> directLink_T30
        graph.add_edges((labels['19'], labels['30']))
        # directLink_T30 -> MetaModelElement_T17
        graph.add_edges((labels['30'], labels['17']))
        
        #===============================================================================
        # Set the output pivots
        #===============================================================================
        # MetaModelElement_T17
        packet.global_pivots['element1'] = graph.vs[labels['17']][Himesis.Constants.GUID]
        # MetaModelElement_T18
        packet.global_pivots['element2'] = graph.vs[labels['18']][Himesis.Constants.GUID]
        
        #===============================================================================
        # Perform the post-action
        #===============================================================================
        try:
            self.action(lambda i: graph.vs[labels[i]], graph)
        except Exception, e:
            raise Exception('An error has occurred while applying the post-action', e)
        #===============================================================================
        # Finally, delete nodes (this will automatically delete the adjacent edges)
        #===============================================================================
        # MT_pre__directLink_T23
        graph.delete_nodes([labels["23"]])
    
